package org.speed.redislock.rw;

import java.awt.Choice;
import java.util.NoSuchElementException;

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.jedis.exceptions.JedisExhaustedPoolException;

public class RedisPool {
	
	private GenericObjectPool<RedisCache> masterPool;
	private GenericObjectPool<RedisCache> salvePool;
	
	public RedisPool(GenericObjectPoolConfig poolConfig,String hostAndPorts){
		String[] nodes = hostAndPorts.split(",");
		for (String node : nodes) {
			String host = node.split(":")[0];
			String port = node.split(":")[1];
			
			RedisNode redisNode = new RedisNode(host, Integer.parseInt(port));
			
		}
	}
	
	public RedisCache getResource(boolean isMaster) {
	    try {
	      return isMaster ? masterPool.borrowObject() : salvePool.borrowObject();
	    } catch (NoSuchElementException nse) {
	      if (null == nse.getCause()) { // The exception was caused by an exhausted pool
	        throw new JedisExhaustedPoolException(
	            "Could not get a resource since the pool is exhausted", nse);
	      }
	      throw new JedisException("Could not get a resource from the pool", nse);
	    } catch (Exception e) {
	      throw new JedisConnectionException("Could not get a resource from the pool", e);
	    }
	}
	
	
}
